package com.broada.apm.logic;

import java.util.ArrayList;
import java.util.List;

import com.broada.apm.indicator.api.ApmClientFactory;
import com.broada.apm.indicator.api.ServiceService;
import com.broada.apm.indicator.api.common.ApmDataIndicatorException;
import com.broada.apm.model.Service;

/**
 * @ClassName: ClearAutoDiscoveryProtocol
 * @Description:
 * @author Hety
 * @date 2015年6月8日 下午3:46:50
 */
public class ClearAutoDiscoveryProtocol {

	/**
	 * @Title: clear
	 * @Description:
	 * @param
	 * @return void
	 * @throws
	 */
	public void clear() {
		List<String> serviceIds = new ArrayList<String>();
		ServiceService service = ApmClientFactory.getApmClient().getServiceService();
		List<Service> focused_service = service.getAllFocusedDisService();
		List<Service> app_service = service.getAllServicesNotCluster();
		if(focused_service != null && focused_service.size() > 0){
			for(Service ser:focused_service){
				if(!serviceIds.contains(ser.getId())){
					serviceIds.add(ser.getId());
				}
			}
		}
		if(app_service != null && app_service.size() > 0){
			for(Service ser:app_service){
				String[] strArry = ser.getId().split("-");
				if(!serviceIds.contains(strArry[strArry.length-1])){
					serviceIds.add(strArry[strArry.length-1]);
				}
			}
		}
		if(serviceIds.size() > 0){
			service.deleteDiscoveryServiceByIds(serviceIds);
		}
	}

	public static void main(String[] args) throws ApmDataIndicatorException {
		ClearAutoDiscoveryProtocol clear = new ClearAutoDiscoveryProtocol();
		clear.clear();
	}
}
